<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport' />

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body class='contrast-red '>
<div>
    <div>
        <button id="connect" onclick="connect();">连接</button>
    </div>
    <div id="conversationDiv">
        <label>输入你的名字</label><input type="text" id="name" />
        <button id="sendName" onclick="sendName();">发送</button>
        <p id="response"></p>
    </div>
</div>


<script src="/static/jquery.min.js" type='text/javascript'></script>
<script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.min.js" type="text/javascript"></script>
<script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.min.js" type="text/javascript"></script>
<script>
    var stompClient = null;



    function connect() {
        var socket = new SockJS('/endpointWisely'); //1
        stompClient = Stomp.over(socket);//2
        stompClient.connect({}, function(frame) {//3
            console.log('开始进行连接Connected: ' + frame);
            stompClient.subscribe('/topic/getResponse/2', function(res){ //4
                var result = JSON.parse(res.body);
                console.log(result);
                showResponse(result.message);
            });
            stompClient.subscribe('/app/marco/2',function (res) {
                console.log(res);
            });
        });
    }


    function disconnect() {
        if (stompClient != null) {
            stompClient.disconnect();
        }
        console.log("Disconnected");
    }

    function sendName() {
        var name = $('#name').val();
        stompClient.send("/app/welcome/2", {}, JSON.stringify({ 'message': name }));//5
    }

    function showResponse(message) {
        var response = $("#response");
        response.html(message);
    }
</script>
</body>
</html>
